package cn.sylinx.common.ext.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import cn.sylinx.common.ext.log.GLog;

public class DateUtil {

	public final static SimpleDateFormat SF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public final static SimpleDateFormat SF1 = new SimpleDateFormat("yyyy-MM-dd");

	public static String getCurrentTime() {
		return getTime(new Date());
	}

	public static String getTime(Date date) {
		return SF.format(date);
	}

	public static String getDate(Date date) {
		return SF1.format(date);
	}

	public static Date getDateFromString(String str) {
		try {
			return SF1.parse(str);
		} catch (ParseException e) {
			return null;
		}
	}

	public static Date getTimeFromString(String str) {
		try {
			return SF.parse(str);
		} catch (ParseException e) {
			return null;
		}
	}

	public static long getDayRange(Date beginDate, Date endDate) {
		long day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
		return day;
	}

	public static long getDayRange(String beginDateStr, String endDateStr) {
		long day = 0;
		java.util.Date beginDate;
		java.util.Date endDate;
		try {
			beginDate = SF1.parse(beginDateStr);
			endDate = SF1.parse(endDateStr);
			day = getDayRange(beginDate, endDate);
		} catch (ParseException e) {
			GLog.error("日期相减异常", e);
		}
		return day;
	}

	public static long getDayRangeWithCurrent(String endDateStr) {

		long day = 0;
		java.util.Date beginDate = new Date();
		java.util.Date endDate;
		try {
			endDate = SF1.parse(endDateStr);
			day = getDayRange(beginDate, endDate);
		} catch (ParseException e) {
			GLog.error("日期相减异常", e);
		}

		return day;
	}

	public static int getNatureMonth(Date d) {

		Calendar c = Calendar.getInstance();
		c.setTime(d);
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH) + 1;
		return year * 100 + month;
	}

	public static int getNatureMonth(String dateStr) {

		Date d = null;
		try {
			d = SF1.parse(dateStr);
		} catch (ParseException e) {
			GLog.error("日期解析异常", e);
		}

		return getNatureMonth(d);
	}

	public static int getCurrentNatureMonth() {
		return getNatureMonth(new Date());
	}

	public static int getNatureMonthBefore(Date d, int dif) {

		Calendar c = Calendar.getInstance();
		c.setTime(d);
		c.add(Calendar.MONTH, -dif);

		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH) + 1;
		return year * 100 + month;
	}

	public static int getNatureMonthBefore(String dateStr, int dif) {

		Date d = null;
		try {
			d = SF1.parse(dateStr);
		} catch (ParseException e) {
			GLog.error("日期解析异常", e);
		}
		return getNatureMonthBefore(d, dif);
	}

	/**
	 * 获取间隔天数的日期
	 * 
	 * @param d
	 * @param dayDif
	 * @return
	 */
	public static Date getDayRange(Date d, int dayDif) {
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		c.add(Calendar.DAY_OF_YEAR, dayDif);
		return c.getTime();
	}

	public static void main(String[] args) {
		System.out.println(getNatureMonthBefore("2020-03-31", 1));
	}

}
